class SoundManager {
	//背景音乐
	private bg_sound:egret.Sound;
	//点击按钮的声音
	private click_sound:egret.Sound;
	//点击文字的声音
	private tapword_sound:egret.Sound;
	//正确的声音
	private right_sound:egret.Sound;
	//错误的声音
	private wrong_sound:egret.Sound;
	//音轨
	private soundChannel:egret.SoundChannel;

	private static shared:SoundManager;
	public static getInstance(){
		if(!SoundManager.shared){
			SoundManager.shared=new SoundManager();
		}
		return SoundManager.shared;
	}

	//构造函数 初始化音频资源加载
	public constructor() {
		this.bg_sound=new egret.Sound();
		this.bg_sound.load("resource/assets/data/sound/Music.mp3");
		this.bg_sound.addEventListener(egret.Event.COMPLETE,()=>{
			this.playBgMusic();
		},this);

		this.click_sound=new egret.Sound();
		this.click_sound.load("resource/assets/data/sound/buttonclick.mp3");

		this.tapword_sound=new egret.Sound();
		this.tapword_sound.load("resource/assets/data/sound/type_word.mp3");

		this.right_sound=new egret.Sound();
		this.right_sound.load("resource/assets/data/sound/right.mp3");

		this.wrong_sound=new egret.Sound();
		this.wrong_sound.load("resource/assets/data/sound/wrong.mp3");
	}
	//播放背景音乐
	private playBgMusic(){
		if(this.bg_sound&&this.isMusic){
			this.soundChannel=this.bg_sound.play(0,0);
		}
	}
	//停止背景音乐
	private stopBgMusic(){
		if(this.soundChannel){
			this.soundChannel.stop();
		}
	}
	//播放点击按钮音效
	public playClick(){
		if(this.isEffect&&this.click_sound){
			this.click_sound.play(0,1);
		}
	}
	//播放点击文字音效
	public playTapWord(){
		if(this.isEffect&&this.tapword_sound){
			this.click_sound.play(0,1);
		}
	}
	//播放正确音效
	public playRight(){
		if(this.isEffect&&this.right_sound){
			this.click_sound.play(0,1);
		}
	}
	//播放错误音效
	public playWrong(){
		if(this.isEffect&&this.wrong_sound){
			this.click_sound.play(0,1);
		}
	}
	//设置是否播放背景音乐
	public set isMusic(val){
		if(val){
			//播放
			egret.localStorage.setItem("isMusic","1");
			this.playBgMusic();
		}else{
			egret.localStorage.setItem("isMusic","0");
			this.stopBgMusic();
		}
	}
	//获取背景音乐是否播放
	public get isMusic(){
		let ret=egret.localStorage.getItem("isMusic");
		if(ret==null||ret==""){
			//如果获取到ret是null或者""说明是程序第一次运行 默认播放声音
			return true;
		}else{
			return ret=="1";
		}
	}
	//设置是否播放音效
	public set isEffect(val){
		if(val){
			//播放
			egret.localStorage.setItem("isEffect","1");
		}else{
			egret.localStorage.setItem("isEffect","0");
		}
	}
	//获取音效是否播放
	public get isEffect(){
		let ret=egret.localStorage.getItem("isEffect");
		if(ret==null||ret==""){
			//如果获取到ret是null或者""说明是程序第一次运行 默认播放声音
			return true;
		}else{
			return ret=="1";
		}
	}
}